SRC_PATH=`pwd`/./source
TARGET_PATH=`pwd`/./target
CURRENT_PATH=`pwd`

#yum install gcc gcc-c++ make git openssl-devel freetype-devel fontconfig-devel
#sudo apt-get install build-essential g++ flex bison gperf ruby perl \
#  libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
#  libpng-dev libjpeg-dev

if [ ! -d "google-coredumper" ]; then
    git clone https://github.com/anatol/google-coredumper.git
fi 

if [ ! -d "phantomjs" ]; then
    git clone git://github.com/ariya/phantomjs.git
fi 

if [ ! -f "zeromq-4.0.4.tar.gz" ]; then
    wget http://download.zeromq.org/zeromq-4.0.4.tar.gz
fi

if [ ! -f "czmq-2.2.0.tar.gz" ]; then
    wget http://download.zeromq.org/czmq-2.2.0.tar.gz
fi

if [ ! -d "jzmq" ]; then
    git clone https://github.com/zeromq/jzmq.git
fi

if [ ! -f "boost_1_55_0.tar.bz2" ]; then
    wget http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.bz2
fi

if [ ! -f "scons-2.3.4.tar.gz" ]; then
    wget http://prdownloads.sourceforge.net/scons/scons-2.3.4.tar.gz
fi

if [ ! -f "libev-4.20.tar.gz" ]; then
    wget http://dist.schmorp.de/libev/libev-4.20.tar.gz
fi

if [ ! -d "$SRC_PATH" ]; then
    mkdir "$SRC_PATH"
fi 

if [ ! -d "$SRC_PATH/zeromq-4.0.4" ]; then
    tar xvf zeromq-4.0.4.tar.gz -C$SRC_PATH
fi 

if [ ! -d "$SRC_PATH/czmq-2.2.0" ]; then
    tar xvf czmq-2.2.0.tar.gz -C$SRC_PATH
fi 

if [ ! -d "$SRC_PATH/boost_1_55_0" ]; then
    tar xvf boost_1_55_0.tar.bz2 -C$SRC_PATH
fi 

if [ ! -d "$SRC_PATH/scons-2.3.4" ]; then
    tar xvf scons-2.3.4.tar.gz -C$SRC_PATH
fi

if [ ! -d "$SRC_PATH/libev-4.20" ]; then
    tar xvf libev-4.20.tar.gz -C$SRC_PATH
fi

if [ ! -d "$SRC_PATH/protobuf" ]; then
    tar xvf ../src/third/protobuf.tar.gz -C$SRC_PATH
fi

if [ ! -d "$TARGET_PATH" ]; then
    mkdir "$TARGET_PATH"
fi 

if [ ! -f "$TARGET_PATH/lib/libcoredumper.so" ]; then
    cd google-coredumper
    ./configure --prefix=`pwd`/../target/
    make -j8
    make install
    cd $CURRENT_PATH
fi

if [ ! -f "$TARGET_PATH/lib/libzmq.so" ]; then
    cd $SRC_PATH/zeromq-4.0.4
    ./configure --prefix=`pwd`/../../target/
    make -j8
    make install
    cd $CURRENT_PATH
fi

if [ ! -f "$TARGET_PATH/lib/libczmq.so" ]; then
    cd $SRC_PATH/czmq-2.2.0
    ./configure --prefix=`pwd`/../../target/ PKG_CONFIG_PATH=`pwd`/../../target/lib/pkgconfig
    make -j8
    make install
    cd $CURRENT_PATH
fi

if [ ! -f "$TARGET_PATH/lib/libjzmq.so" ]; then
    cd jzmq
    ./autogen.sh
    ./configure --prefix=`pwd`/../target/ PKG_CONFIG_PATH=`pwd`/../target/lib/pkgconfig
    make -j8
    make install
    cd $CURRENT_PATH
fi


if [ ! -f "$TARGET_PATH/lib/libboost_filesystem.so" ]; then
    cd $SRC_PATH/boost_1_55_0
    ./bootstrap.sh
    ./b2 --prefix=`pwd`/../../target/ --with-filesystem --with-program_options --with-system --with-thread --with-signals --with-regex install
    cd $CURRENT_PATH
fi

scons -v
if [ $? -eq 127 ]; then
    cd $SRC_PATH/scons-2.3.4
    sudo python setup.py install
    cd $CURRENT_PATH
fi

if [ ! -f "$TARGET_PATH/lib/libev.so" ]; then
    cd $SRC_PATH/libev-4.20
    ./configure --prefix=`pwd`/../../target/
    make -j8
    make install
    cd $CURRENT_PATH
fi

if [ ! -f "$TARGET_PATH/lib/libprotobuf.so" ]; then
    cd $SRC_PATH/protobuf
    ./configure --prefix=`pwd`/../../target/
    make -j8
    make install
    cd $CURRENT_PATH
fi

if [ ! -f "$CURRENT_PATH/phantomjs/bin/phantomjs" ]; then
    cp patch/phantompatch.tar.gz phantomjs/
    cd phantomjs
    git checkout 1.9
    tar xvf phantompatch.tar.gz
    ./build.sh
    cd $CURRENT_PATH
fi

if [ ! -d "../src/qt" ]; then
    cp -a phantomjs/src/qt ../src/
fi

#### third

cd ../src/third
if [ ! -d "jsoncpp-src-0.5.0" ]; then
    tar xvf jsoncpp-src-0.5.0.tar.gz
fi

if [ ! -f "libjson.a" ]; then
    cd jsoncpp-src-0.5.0
    scons platform=linux-gcc
    cp `find libs -name "libjson_linux-gcc*.a"` ../libjson.a
    cd -
fi

if [ ! -d "$TARGET_PATH/include/json" ]; then
    cd jsoncpp-src-0.5.0
    cp -a include/json $TARGET_PATH/include/
    cd -
fi

if [ ! -d "zookeeper-3.4.6" ]; then
    tar xvf zookeeper-3.4.6.tar.gz
fi

if [ ! -f "$TARGET_PATH/lib/libzookeeper_mt.so" ]; then
    cd zookeeper-3.4.6/src/c
    ./configure --prefix=$CURRENT_PATH/target/
    make -j8
    make install
    cd $CURRENT_PATH
fi
cd $CURRENT_PATH

### end third

### build spiderkit
if [ ! -f "$CURRENT_PATH/../bin/spiderkit" ]; then
    cd $CURRENT_PATH/../
    ./build.sh --build-qt --proto
fi
### end spiderkit

### build monitor
if [ ! -f "$CURRENT_PATH/../src/monitor/src/spiderkit-monitor/target/spiderkit-monitor-0.0.1.jar" ]; then
    cd $CURRENT_PATH
    cd ../src/monitor/src/spiderkit-monitor/lib
    mvn install:install-file -DgroupId=com.google -DartifactId=protobuf-java -Dversion=3.0.0-pre -Dpackaging=jar -Dfile=protobuf-java-3.0.0-pre.jar
    cd ..
    mvn assembly:assembly
fi
### end monitor

### build java spider app
if [ ! -f "$CURRENT_PATH/../src/java/src/skit-client/target/skit-client-0.0.1.jar" ]; then
    cd $CURRENT_PATH
    cd ../src/java/src/skit-client/
    mvn clean package install
fi

if [ ! -f "$CURRENT_PATH/../src/java/src/spider-plugin/target/spider-plugin-0.0.1.jar" ]; then
    cd $CURRENT_PATH
    cd ../src/java/src/spider-plugin/
    mvn clean package install
fi

if [ ! -f "$CURRENT_PATH/../src/java/src/spider-data-extract/target/spider-data-extract-0.0.1-SNAPSHOT.jar" ]; then
    cd $CURRENT_PATH
    cd ../src/java/src/spider-data-extract/
    mvn clean
    mvn assembly:assembly
fi

if [ ! -f "$CURRENT_PATH/../src/java/src/spider-schedule/target/spider-schedule-0.0.1.jar" ]; then
    cd $CURRENT_PATH
    cd ../src/java/src/spider-schedule/
    mvn clean
    mvn assembly:assembly
fi
### end monitor



